// lib/models/review.dart
class Review {
  final String id; // 数据标识，对应 _id
  final String content; // 评价内容
  final double rating; // 评分
  final String? reply; // 商家回复，可选
  final String? owner; // 所有人，系统字段
  final List<String>? departmentList; // 所属部门，系统字段
  final int createdAt; // 创建时间，毫秒时间戳
  final String? createBy; // 创建人，系统字段
  final String? updateBy; // 修改人，系统字段
  final String? openid; // 记录创建者，系统字段
  final String? spuId; // 关联的 SPU ID
  final String? orderItemId; // 关联的订单项 ID
  final int updatedAt; // 更新时间，毫秒时间戳

  Review({
    required this.id,
    required this.content,
    required this.rating,
    this.reply,
    this.owner,
    this.departmentList,
    required this.createdAt,
    this.createBy,
    this.updateBy,
    this.openid,
    this.spuId,
    this.orderItemId,
    required this.updatedAt,
  });

  factory Review.fromJson(Map<String, dynamic> json) {
    return Review(
      id: json['_id'] as String,
      content: json['content'] as String,
      rating: (json['rating'] as num).toDouble(),
      reply: json['reply'] as String?,
      owner: json['owner'] as String?,
      departmentList:
          (json['_departmentList'] as List<dynamic>?)?.cast<String>(),
      createdAt: json['createdAt'] as int? ?? 0,
      createBy: json['createBy'] as String?,
      updateBy: json['updateBy'] as String?,
      openid: json['_openid'] as String?,
      spuId:
          json['spu'] != null
              ? (json['spu'] as Map<String, dynamic>)['_id'] as String?
              : null,
      orderItemId:
          json['order_item'] != null
              ? (json['order_item'] as Map<String, dynamic>)['_id'] as String?
              : null,
      updatedAt: json['updatedAt'] as int? ?? 0,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      '_id': id,
      'content': content,
      'rating': rating,
      'reply': reply,
      'owner': owner,
      '_departmentList': departmentList,
      'createdAt': createdAt,
      'createBy': createBy,
      'updateBy': updateBy,
      '_openid': openid,
      'spu': spuId != null ? {'_id': spuId} : null,
      'order_item': orderItemId != null ? {'_id': orderItemId} : null,
      'updatedAt': updatedAt,
    };
  }
}
